package com.suoluo.zenadmin.service.crawler.resolver;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import com.suoluo.zenadmin.service.crawler.bean.HotelReviewRecordBean;



class HotelsCrawlerResolver extends AbstractCrawlerResolver{

	public HotelsCrawlerResolver(CrawlerResovlerTask task) {
		super(task);
	}

	@Override
	protected int resolveReviewCount(Document doc) {
		String reviewCount = doc.select("div.no_score").select("ul.filters").select("li.active").select("span.cat").first().ownText();
		int start = reviewCount.indexOf("(");
		int end = reviewCount.indexOf(")");
		reviewCount = reviewCount.substring(start+1,end);
//		String reviewCountStr = doc.select("a.reviews").text();//(439 条点评)
//		String reviewCountNum = reviewCountStr.replaceAll("[^\\d]", "");
		return Integer.valueOf(reviewCount);
	}

	@Override
	protected double resolveReviewScore(Document doc) {
		String reviewScoreStr = doc.select("div.overall").select("span.rating").select("strong").text();
		return Double.valueOf(reviewScoreStr);
	}

	@Override
	protected double resolveReviewFullScore(Document doc) {
		//默认5分制
		return 5;
	}

	@Override
	protected String resolveReviewLevel(Document doc) {
		return doc.select("div.overall").select("div.badge").text();
	}

	@Override
	protected int resolveReviewRank(Document doc) {
		//默认没有排名
		return -1;
	}

	@Override
	protected List<HotelReviewRecordBean> resolveReviewRecords(Document doc) {
		Elements eles = doc.select("div.reviews").select("div.hreview");
		Iterator<Element> iter = eles.iterator();
		List<HotelReviewRecordBean> records = new ArrayList<HotelReviewRecordBean>();
		int temp = 0;
		while (iter.hasNext()) {
			if (temp == 5) {
				break;
			}
			Element ele = iter.next();
		
			HotelReviewRecordBean record = new HotelReviewRecordBean();
			//评论标题
			String title = ele.select("div.quote").select("q.summary").text();
			//评论描述
			String desc = ele.select("blockquote.description").first().ownText();
			//没有评论直接过滤掉，继续抓取下一条
			if (!StringUtils.hasLength(title) || !StringUtils.hasLength(desc)){
				continue;
			}
			
			record.setRecordTitle(title);
			//评论描述
			record.setRecordDesc(desc);
			
			//评论作者
			String author = ele.select("div.reviewer").select("span").first().text();
			author = author.substring(0,author.lastIndexOf(" "));
			if (!StringUtils.hasLength(author)){
				author = "一个旅客";
			}
			record.setRecordAutor(author);
			
			//评论时间
			String tim = ele.select("div.data").text();
			int sta = tim.indexOf("-");
			if(sta != -1)
			{
				tim = tim.substring(sta+1);
			}
			record.setRecordTime(tim.trim());
			//评分
			String score = ele.select("span.rating").select("strong").text().trim();
			record.setRecordScore(Double.valueOf(score));
			
			//默认5.0分
			record.setRecordFullScore(5.0);
			temp++;
			records.add(record);
		}

		return records;
	}

//	public static void main(String []args){
//		CrawlerResovlerTask task = new CrawlerResovlerTask(null);
//		task.setReviewUrl("http://www.hotels.cn/ho122780/le-bristol-paris-ba-li-fa-guo/#reviews");
//		HotelsCrawlerResolver r = new HotelsCrawlerResolver(task);
//		r.doResolver();
//	}
//	public static void main(String []args){
//		String reviewCountStr = "(439 条点评)";
//		String reviewCountNum = reviewCountStr.replaceAll("[^\\d]", "");
//		System.out.println(reviewCountNum);
//	}
}
